﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;

namespace SchoolManagementSystem
{
    public partial class MuridBaru : Form
    {
        public MuridBaru()
        {
            InitializeComponent();
        }

        private void MuridBaru_Load(object sender, EventArgs e)
        {
            // Load School Session
            DateTime date = DateTime.Now;
            if (date.Month < 7)
            {
                num1.Value = date.Year - 1;
                num2.Value = num1.Value + 1;
            }
        }

        private void num1_ValueChanged(object sender, EventArgs e)
        {
            num2.Value = num1.Value + 1;
        }

        private void num2_ValueChanged(object sender, EventArgs e)
        {
            num1.Value = num2.Value - 1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;

            string session = this.num1.Value.ToString() + " - " + this.num2.Value.ToString();

            // string query = "SELECT * FROM Student AS s WHERE s.stu_regsession='" + session + "'";

            string query = "SELECT CONVERT(VARCHAR(50),stu_regdate,106) AS stu_registration, stu_name, stu_gender, stu_birthplace, CONVERT(VARCHAR(50),stu_birthdate,106), par_name, par_address, par_job, stu_desc FROM Student AS s INNER JOIN Parent ON s.par_id=Parent.par_id WHERE s.stu_regsession='" + session + "'";


            DataTable dTable = Database.getInstance().query(query);
            ReportDocument rptDoc = new ReportDocument();
            alwardahDataSet ds = new alwardahDataSet();

            for (int i = 0; i < dTable.Rows.Count; i++)
            {
                string buffer = dTable.Rows[i].ItemArray[3].ToString();
                buffer += ", ";
                buffer += dTable.Rows[i].ItemArray[4].ToString();
                dTable.Rows[i]["stu_birthplace"] = buffer;

                string regdate = dTable.Rows[i].ItemArray[0].ToString();
                dTable.Rows[i]["stu_registration"] = regdate;      
            }

            ds.Tables["StudentByParent"].Merge(dTable);

            string appPath = (System.IO.Path.GetDirectoryName(Application.ExecutablePath));
            rptDoc.Load(appPath + "\\Report\\ReportAD3.rpt");
            rptDoc.SetDataSource(ds);

            // siapkan report viewer
            ReportForm f1 = new ReportForm(rptDoc);
            f1.ShowDialog();
            
            Cursor = Cursors.Default; 

            /* 
            // ganti cursor
            Cursor = Cursors.WaitCursor;

            string query = "SELECT CONVERT(VARCHAR(50),stu_regdate,106) AS stu_registration, stu_name, stu_gender, stu_birthplace, CONVERT(VARCHAR(50),stu_birthdate,106), par_name, par_address, par_job, stu_desc FROM Student AS s INNER JOIN Parent ON s.par_id=Parent.par_id";
            DataTable dTable = Database.getInstance().query(query);
            ReportDocument rptDoc = new ReportDocument();
            alwardahDataSet ds = new alwardahDataSet();

            for (int i = 0; i < dTable.Rows.Count; i++)
            {
                string buffer = dTable.Rows[i].ItemArray[3].ToString();
                buffer += ", ";
                buffer += dTable.Rows[i].ItemArray[4].ToString();
                dTable.Rows[i]["stu_birthplace"] = buffer;

                string regdate = dTable.Rows[i].ItemArray[0].ToString();
                dTable.Rows[i]["stu_registration"] = regdate;
            }

            ds.Tables["StudentByParent"].Merge(dTable);

            // string appPath = (System.IO.Path.GetDirectoryName(Application.ExecutablePath) + "\\SimpleReport1.rpt");

            string appPath = (System.IO.Path.GetDirectoryName(Application.ExecutablePath));
            rptDoc.Load(appPath + "\\Report\\ReportAD3.rpt");
            rptDoc.SetDataSource(ds);

            // siapkan report viewer
            ReportForm f1 = new ReportForm(rptDoc);
            f1.ShowDialog();

            // ganti cursor
            Cursor = Cursors.Default; 
             * */ 



        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }
    }
}
